<?php
/*
Plugin Name: 百度推送
Plugin URI: https://www.beizigen.com/post/wordpress-baidu-included-automatic-push-plug-in/
Description: 发布新的文章自动推送给百度，缩短爬虫发现网站链接的时间。
Version: 1.0.0
Author: 背字根
Author URI: https://www.beizigen.com/
Text Domain: baidu-submit
License: GPL v3 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/

require_once('functions.php');

//添加菜单
add_action('admin_menu', function () {
	add_options_page('百度推送', '百度推送', 'administrator', 'baidu-submit', 'baidu_submit_control');
});

//插件页面设置链接
add_filter('plugin_action_links', function ($links, $file) {
	if ($file == plugin_basename(__FILE__)) {
		array_unshift($links, '<a href="options-general.php?page=baidu-submit">设置</a>');
	}
	return $links;
}, 10, 2);

//卸载插件
register_uninstall_hook(__FILE__, 'baidu_submit_uninstall');

function baidu_submit_control()
{
	if (isset($_POST['action']) && 'save' == $_POST['action']) {
		if (!empty($_POST['auto_submit'])) {
			update_option('baidu_submit_auto', 'on');
		} else {
			delete_option('baidu_submit_auto');
		}

		$submit_key = sanitize_text_field($_POST['submit_key']);
		update_option('baidu_submit_key', $submit_key);
		echo '<div class="notice is-dismissible updated">保存设置成功！</div>';
	}

	if (isset($_POST['action']) && 'submit' == $_POST['action']) {
		$urls = trim($_POST['urls']);
		$result = baidu_submit_send($urls);
		if (is_int($result)) {
			echo '<div class="notice is-dismissible updated">推送到百度成功，共推送' . $result . '条URL</div>';
		} else {
			echo '<div class="notice is-dismissible error">推送失败，错误信息：' . $result . '</div>';
		}
	}
?>

	<style type="text/css">
		#wpwrap .notice {
			margin: 20px 20px 20px 0;
			padding: 1em;
		}

		#wpwrap .notice.success {
			color: #155724;
			background-color: #d4edda;
			border-color: #c3e6cb;
		}

		#wpwrap .notice.error {
			color: #856404;
			background-color: #fff3cd;
			border-color: #ffeeba;
		}

		#auto-submit-urls td {
			padding: 0;
		}
	</style>

	<form class="entry" method="post" action="<?php echo admin_url("options-general.php?page=baidu-submit"); ?>">
		<h2>推送设置</h2>
		<p>网站域名需要先在<a href="https://ziyuan.baidu.com/" target="_blank" rel="noopener">百度搜索资源平台</a>添加验证。</p>
		<table class="form-table">
			<tr>
				<th scope="row">自动推送</th>
				<td>
					<label>
						<input type="checkbox" id="auto-submit" name="auto_submit" value="on" <?php echo get_option('baidu_submit_auto') ? ' checked' : ''; ?>> 发布新的文章是否自动推送到百度，推送大量的垃圾内容可能导致失去推送权限。
					</label>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="submit-key">推送密钥</label></th>
				<td>
					<input id="submit-key" name="submit_key" class="regular-text code" type="text" value="<?php echo get_option('baidu_submit_key'); ?>" required>
					<p class="description">在<a href="https://ziyuan.baidu.com/" target="_blank" rel="noopener">百度搜索资源平台</a>获取。</p>
				</td>
			</tr>
		</table>
		<p class="submit">
			<input type="submit" class="button-primary" name="action" value="保存设置">
			<input type="hidden" name="action" value="save">
		</p>
	</form>

	<form id="auto-submit-urls" class="entry" method="post" action="<?php echo admin_url("options-general.php?page=baidu-submit"); ?>">
		<h2>手动推送</h2>
		<p>每日至多提交10万条有价值的内容，推送大量垃圾内容可能导致失去推送权限。</p>
		<table class="form-table">
			<tr>
				<td>
					<textarea name="urls" rows="5" cols="50" id="urls" class="large-text code" required></textarea>
					<p class="description">要推送给百度的网址，每行一个。</p>
				</td>
			</tr>
		</table>
		<p class="submit">
			<input type="submit" class="button" name="action" value="推送到百度">
			<input type="hidden" name="action" value="submit">
		</p>
	</form>

<?php } ?>